﻿namespace com.stifel.Global
{
    using System.Linq;
    using System.Reflection;
    using System.Xml;

    using com.stifel.Extensions.AssemblyExtensions;
    using com.stifel.Extensions.SharePointExtensions;
    using com.stifel.Extensions.StringExtensions;
    using com.stifel.Extensions.SymbolExtensions;
    using com.stifel.GlobalDao;
    using com.stifel.SharePoint;

    using Microsoft.SharePoint;

    public class USStates_Definition : BaseDefinition
    {
        #region Fields

        const string LOOKUPSITECOLUMNS = "USStates.Lookup.fields.xml";
        const string SITECOLUMNS = "USStates.fields.xml";

        #endregion Fields

        #region Constructors

        public USStates_Definition( SPFeatureReceiverProperties spFeatureReceiverProperties )
            : base(spFeatureReceiverProperties)
        {
        }

        ~USStates_Definition( )
        {
            Dispose( false );
        }

        #endregion Constructors

        #region Methods

        public override void Create( )
        {
            base.Create( );
            CreateSiteColumns( getFieldDefinitionsAsXml( SITECOLUMNS ) );
            CreateContentType( getContentTypeDefn( ) );
            CreateList( getListDefn( ) );
            CreateSiteColumns( getFieldDefinitionsAsXml( LOOKUPSITECOLUMNS ) );
            loadList( );
        }

        public override void Delete( )
        {
            base.Delete( );
            DeleteList( getListDefn( ), true );
            DeleteContentType( getContentTypeDefn( ) );
            DeleteSiteColumns( getFieldDefinitionsAsXml( LOOKUPSITECOLUMNS ) );
            DeleteSiteColumns( getFieldDefinitionsAsXml( SITECOLUMNS ) );
        }

        XmlNodeList getContentAsXml( string resource )
        {
            XmlNodeList fields = null;
            string s = Assembly.GetExecutingAssembly( ).GetFullyQualifiedManifestResourceName( resource );
            string xml = Assembly.GetExecutingAssembly( ).GetManifestResourceAsString( s );
            if ( !string.IsNullOrEmpty( xml ) )
            {
                XmlDocument d = new XmlDocument( );
                d.LoadXml( xml );
                XmlNode n = d.SelectSingleNode( "USStates" );
                fields = n.SelectNodes( "USState" );
            }
            return fields;
        }

        ContentTypeDefn getContentTypeDefn( )
        {
            USStates c = null;
            return new ContentTypeDefn
            {
                Name = Item.GetContentTypeName<USStates>( ),
                ContentTypeId = Item.GetContentTypeId<USStates>( ),
                Group = "General Content Types",
                Description = "Defines a state or possession name and USPS abbreviation of the United States.",
                Fields = new FieldDefn[ ] {
                    new FieldDefn{
                        Name = Item.GetColumnName<USStates>( c.GetPropertySymbol( o => o.USState ))
                    },
                    new FieldDefn{
                        Name = Item.GetColumnName<USStates>( c.GetPropertySymbol( o => o.USStateAbbreviation ) ),
                        Required = true
                    },
                    new FieldDefn{
                        Name = Item.GetColumnName<USStates>( c.GetPropertySymbol( o => o.USStateActive ) ),
                        Required = true
                    }
                }.Distinct( ).ToList( )
            };
        }

        XmlNodeList getFieldDefinitionsAsXml( string resource )
        {
            XmlNodeList fields = null;
            string s = Assembly.GetExecutingAssembly( ).GetFullyQualifiedManifestResourceName( resource );
            string xml = Assembly.GetExecutingAssembly( ).GetManifestResourceAsString( s );
            if ( !string.IsNullOrEmpty( xml ) )
            {
                XmlDocument d = new XmlDocument( );
                d.LoadXml( xml );
                XmlNode n = d.SelectSingleNode( "Fields" );
                fields = n.SelectNodes( "Field" );
            }
            return fields;
        }

        ListDefn getListDefn( )
        {
            return new ListDefn
            {
                RootFolderName = USStatesDao.ListName,
                Title = "US States and Possessions",
                Description = "Contains the official United States Postal Service abbreviations for states and possessions",
                ContentTypes = new string[ ] {
                    Item.GetContentTypeName<USStates>()
                }.ToList( )
            };
        }

        void loadList( )
        {
            USStatesDao dao = new USStatesDao( Properties.GetWeb( ).Url );
            dao.Clear( );
            XmlNodeList states = getContentAsXml( "USStates.xml" );
            foreach ( XmlNode state in states )
            {
                string name = string.Empty;
                string abbrev = string.Empty;
                bool active = false;
                {
                    XmlAttribute a = state.Attributes[ "Name" ];
                    name = a != null ? a.Value : string.Empty;
                }
                {
                    XmlAttribute a = state.Attributes[ "Abbreviation" ];
                    abbrev = a != null ? a.Value : string.Empty;
                }
                {
                    XmlAttribute a = state.Attributes[ "Active" ];
                    active = a != null ? a.Value.ToBoolean( ) : false;
                }
                if ( !string.IsNullOrEmpty( name ) )
                {
                    USStates s = new USStates
                    {
                        Title = name,
                        USStateAbbreviation = abbrev,
                        USStateActive = active
                    };
                    dao.Add( s );
                }
            }
        }

        #endregion Methods
    }
}